package trocabi.printer;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Transparency;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.imageio.ImageIO;
import org.krysalis.barcode4j.impl.code128.Code128Bean;
import org.krysalis.barcode4j.output.java2d.Java2DCanvasProvider;
import org.krysalis.barcode4j.tools.UnitConv;
import trocabi.model.Article;
import trocabi.service.ArticleServices;

public class Barcode {
    private static final Logger    logger = Logger.getLogger("trocabi.printer.DBManager");
    private String ref;
    private String prix;
    private String type;
    private String pour;
    private String aspect;
    private String date;
    private String couleur;
    
    // private static int sizeX = 500; // TODO: constante!
    // private static int sizeY = 130;

    private static int sizeX = 195; // TODO: constante!
    private static int sizeY = 750;
    public Barcode(Article article) {
        pour = Article.codeToGenre(article.getCodeGenre());
        aspect = Article.codeToAspect(article.getCodeAspect());
        couleur = article.getCouleur();
        type = Article.codeToType(article.getCodeType());
        ref = ArticleServices.idToString(article);
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        date = "Déposé le " + sdf.format(article.getDateDepot());
        
        prix=String.format("%3.2f €", article.getPrixAchat()+(article.getPrixAchat()*article.getTxMarge()));
        // int iprix = art.getMarge() + art.getPrix_achat(); // TODO: set price
        // prix = (String.valueOf((iprix - iprix % 100) / 100) + "," + String.valueOf((iprix % 100 - iprix % 10) / 10) + String.valueOf(iprix % 10)) + " €";;
    }
   
    
     public BufferedImage toBufferedImg() {
        BufferedImage bimg = new BufferedImage(sizeX, sizeY, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = bimg.createGraphics();
       // graphics.rotate(Math.toRadians(45)); 
        // 
        AffineTransform at = new AffineTransform();
        at.setToRotation(Math.toRadians(90), 0 + (sizeX / 2), 0 + (sizeY / 2));
        at.translate(-280,260);
        at.scale(1.5, 1.5);
        graphics.setTransform(at);
        
        
        graphics.setColor(Color.white);
        // graphics.fillRect(0, 0, sizeX, sizeY);
        graphics.fillRect(0, 0, 800, 600);
        graphics.setColor(Color.WHITE);
        // graphics.setColor(Color.BLACK);
        // graphics.drawRect(0, 0, sizeX - 1, sizeY - 1);
        graphics.setColor(Color.WHITE);
        graphics.drawRect(0, 0, 1, sizeY);
        graphics.setColor(Color.BLACK);
       
        Code128Bean bean = new Code128Bean();
        final int dpi = 15;
        bean.setModuleWidth(UnitConv.in2mm(1.6f / dpi));
        bean.setHeight(85);
        bean.doQuietZone(true);
        BufferedImage bcodebarre = new BufferedImage(295, 100, BufferedImage.TYPE_INT_RGB);
        Graphics2D gcodebarre = bcodebarre.createGraphics();
        gcodebarre.setColor(Color.white);
        gcodebarre.fillRect(0, 0, 295, 100);
        gcodebarre.setColor(Color.black);
        //gcodebarre.drawRect(0, 0, 279, 99);//A ENLEVER, EMPECHE RECONNAISSANCE
        gcodebarre.drawString(ref, 5, 92);
        Java2DCanvasProvider canvas = new Java2DCanvasProvider(gcodebarre, 0);
        bean.generateBarcode(canvas, ref);
        graphics.drawImage(bcodebarre, 5, 15, null);
       
        Font font = new Font(Font.DIALOG, Font.PLAIN, 15);
        Font font2 = new Font(Font.DIALOG, Font.PLAIN, 30);
        graphics.setFont(font2);
        graphics.drawString(prix, 305, 110);
        graphics.setFont(font);
        graphics.drawString(type, 305, 30);
        graphics.drawString(pour, 410, 30);
        graphics.drawString(couleur, 305, 50);
        graphics.drawString(aspect, 410, 50);
        graphics.drawString(date, 305, 70);
        
        // graphics.draw(rectangle);
        // graphics.dispose();
        return bimg;
    }
     
    
     public BufferedImage copyBufferedImage(BufferedImage source) {
        ColorModel cm = source.getColorModel();
        boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
        WritableRaster raster = source.copyData(null);
        return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}
    public void toBmpFile(String filename) {
        try {
            System.out.println("Write file "+filename); // TODO:del
            BufferedImage image=toBufferedImg(); 
            ImageIO.write(image, "BMP", new File(filename));
        } catch (IOException e) {
           logger.severe(String.format("%s %s",e.getLocalizedMessage(),e.getMessage()));
        }
    }
    
    public static List<Barcode> generateBarcodeList(List<Article> articles) {
        List<Barcode> l = new ArrayList<>();
        for (int i = 0; i < articles.size(); i++) {
            l.add(new Barcode(articles.get(i)));
        }
        return l;
    }

    public static int getSizeX() {
        return sizeX;
    }

    public static int getSizeY() {
        return sizeY;
    }
}
